<!--  -->
<template>
  <div>
    <div class="page-modal">
      <el-dialog
        title="新建用户"
        v-model="DialogVisible"
        destroy-on-close
        width="30%"
        center
      >
        <wxform
          v-bind="modalConfig"
          v-model="formData"
          :showFooter="false"
          @updatemodelValue="updatemodelValue"
        ></wxform>
        <slot></slot>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="DialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="handleComfirmClick"
              >确 定</el-button
            >
          </span>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { defineComponent, ref, watch } from 'vue'
import { useStore } from 'vuex'
import wxform from '../../../base-ui/form/index.ts'
export default defineComponent({
  components: {
    wxform
  },
  props: {
    modalConfig: {
      type: Object,
      require: true
    },
    defualtInfo: {
      type: Object,
      default: () => ({})
    },
    pageName: {
      type: String,
      require: true
    },
    otherInfo: {
      type: Object,
      default: () => ({})
    }
  },
  emits: ['handleComfirmClick', 'modalData'],
  setup(props, { emit }) {
    const DialogVisible = ref(false) //默认弹窗关闭
    const formData = ref({})
    watch(
      () => props.defualtInfo,
      (newvalue) => {
        for (const item of props.modalConfig.formItems) {
          formData.value[`${item.field}`] = newvalue[`${item.field}`]
        }
      }
    )
    // 点击确定按钮的逻辑
    const store = useStore()
    const updatemodelValue = (newValue) => {
      //监听数据更新，并进行赋值
      formData.value = newValue
    }
    const handleComfirmClick = () => {
      emit('handleComfirmClick', formData.value)
      DialogVisible.value = false
      if (Object.keys(props.defualtInfo).length) {
        // 编辑
        store.dispatch('system/editPageDataAction', {
          pageName: props.pageName,
          editData: { ...formData.value, ...props.otherInfo },
          id: props.defualtInfo.id
        })
      } else {
        // 新建
        store.dispatch('system/createPageDataAction', {
          pageName: props.pageName,
          newData: { ...formData.value, ...props.otherInfo }
        })
      }
    }
    return { DialogVisible, formData, handleComfirmClick, updatemodelValue }
  }
})
</script>
<style lang="less" scoped></style>
